//
//  MTSettingViewController.m
//  ShiShiBang
//
//  Created by 宋宝强 on 16/9/21.
//  Copyright © 2016年 motianjituan. All rights reserved.
//


#import "MTSettingViewController.h"
#import <SDImageCache.h>
#import <SDWebImageManager.h>
#import "UIViewController+JTNavigationExtension.h"
#import "MTAboutViewController.h"
#import "MTProfile.h"
@interface MTSettingViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
/** 数据数组 */
@property (nonatomic, strong) NSArray *dataList;

/** 记录清空缓存的index */
@property (nonatomic, strong) NSIndexPath *path;
@end

@implementation MTSettingViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self initNavigation];
    [self setUpExitTtn];
    self.jt_fullScreenPopGestureEnabled = YES;
}

- (NSArray *)dataList{
    if (!_dataList) {
        NSMutableDictionary *profile = [NSMutableDictionary dictionary];
        profile[@"title"] = @"个人资料";
        profile[@"controller"] = [MTProfile class];
        
        
        NSMutableDictionary *cleanCache = [NSMutableDictionary dictionary];
        cleanCache[@"title"] = @"清空缓存";
//        cleanCache[@"icon"] = @"img_cache";
        
        NSMutableDictionary *about = [NSMutableDictionary dictionary];
        about[@"title"] = @"关于实事帮";
//        liCai[@"icon"] = @"buyread";
        about[@"controller"] = [MTAboutViewController class];
        
        NSArray *section1 = @[profile];
        NSArray *section2 = @[cleanCache, about];
        
        _dataList = [NSArray arrayWithObjects:section1, section2, nil];
    }
    return _dataList;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStyleGrouped];
        //        _tableView.y = -20;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

//刷新tableview
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.tableView reloadData];
}


#pragma mark - custom action
- (NSString *)caCheSize
{
    NSString *detailTitle = nil;
    CGFloat size = [SDImageCache sharedImageCache].getSize;
    
    if (size > 1024 * 1024){
        detailTitle = [NSString stringWithFormat:@"%.02fM",size / 1024 / 1024];
    }else if (size > 1024){
        detailTitle = [NSString stringWithFormat:@"%.02fKB",size / 1024];
    }else{
        detailTitle = [NSString stringWithFormat:@"%.02fB",size];
    }
    
    return detailTitle;
}

#pragma mark - UITableViewDelegate, UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataList.count ;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataList[section] count];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *ID = @"mineCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    NSDictionary *dict = self.dataList[indexPath.section][indexPath.row];
    cell.textLabel.text = dict[@"title"];
    cell.imageView.image = [UIImage imageNamed:dict[@"icon"]];
    
    
    cell.selected = YES;
    
    if ([cell.textLabel.text isEqualToString:@"清空缓存"]){
        
        cell.accessoryView = [[UIView alloc] init];
        cell.detailTextLabel.text = [self caCheSize];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    
    if (self.dataList[indexPath.section][indexPath.row][@"controller"]){
        UIViewController *vc = [[self.dataList[indexPath.section][indexPath.row][@"controller"] alloc] init];
        
        vc.title = self.dataList[indexPath.section][indexPath.row][@"title"];
        
        //        vc.view.backgroundColor = XLColor(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255));
        
        
        [self.navigationController pushViewController:vc animated:YES];
        self.navigationController.navigationBar.hidden = NO;
    }else{
        
        
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        
        [[SDImageCache sharedImageCache] clearDisk];
        [[SDWebImageManager sharedManager] cancelAll];
        
        cell.detailTextLabel.text = [self caCheSize];
    }
    
    
}

//导航栏内容设置
-(void)initNavigation {
    
    self.navigationItem.title = @"设置";
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"back"] highImage:[UIImage imageNamed:@"back anxia"] target:self action:@selector(backClick)];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:UIColorFromRGB(0xff45c75a)]  forBarMetrics:UIBarMetricsDefault];
    NSMutableDictionary *attr = [NSMutableDictionary dictionary];
    attr[NSFontAttributeName] = [UIFont boldSystemFontOfSize:17];
    attr[NSForegroundColorAttributeName] = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:attr];
    
}


#pragma mark -添加退出按钮

- (void)setUpExitTtn {
    
    UIButton *exitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [exitBtn setBackgroundImage:[UIImage imageNamed:@"button_normal"] forState:UIControlStateNormal];
    [exitBtn setBackgroundImage:[UIImage imageNamed:@"button-press"] forState:UIControlStateSelected];
    [exitBtn setTitle:@"退出当前账号" forState:UIControlStateNormal];
    [exitBtn sizeToFit];
    exitBtn.center = CGPointMake(MTScreenW * 0.5, MTScreenH * 0.5);
    [self.tableView addSubview:exitBtn];
    [exitBtn addTarget:self action:@selector(exitClick) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark ------点击了退出
-(void)exitClick {
    NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
    
    
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:token, @"token", nil];
    
  
    [MTHttpTools post:MTBaseURL(@"exit.do") params:dict success:^(id responseObj) {
        
        [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"token"];
    } failure:^(NSError *error) {
        
    }];
}
-(void)backClick {
    [self.navigationController popViewControllerAnimated:YES];
}
@end